package com.credit.mola.app.utils.sysdata.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.ImageFormat
import android.graphics.Point
import android.hardware.Sensor
import android.hardware.SensorManager
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Build
import android.view.WindowManager
import org.json.JSONArray
import org.json.JSONObject
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import kotlin.math.pow
import kotlin.math.sqrt

object SysData3Util {

    fun getData3Type1(context: Context): String {
        val jsonArray = JSONArray()
        try {
            val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
            val sensors = sensorManager.getSensorList(Sensor.TYPE_ALL)
            for (sensor in sensors) {
                val jsonObject = JSONObject().apply {
                    put("type", sensor.type.toString())
                    put("name", sensor.name.verifyStr())
                    put("version", sensor.version.toString())
                    put("maxRange", sensor.maximumRange.toString().verifyStr())
                    put("vendor", sensor.vendor.verifyStr())
                    put("minDelay", sensor.minDelay.toString().verifyStr())
                    put("power", sensor.power.toString().verifyStr())
                    put("resolution", sensor.resolution.toString().verifyStr())
                }
                jsonArray.put(jsonObject)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return jsonArray.toString()
    }

    private fun getData3Type2(): Boolean {
        val buildTags = Build.TAGS
        return buildTags != null && buildTags.contains("test-keys")
    }

    private fun getData3Type3(): Boolean {
        val paths = arrayOf(
            "/system/app/Superuser.apk",
            "/sbin/su",
            "/system/bin/su",
            "/system/xbin/su",
            "/data/local/xbin/su",
            "/data/local/bin/su",
            "/system/sd/xbin/su",
            "/system/bin/failsafe/su",
            "/data/local/su"
        )
        return paths.any { File(it).exists() }
    }

    private fun getData3Type4(): Boolean {
        var process: Process? = null
        return try {
            process = Runtime.getRuntime().exec(arrayOf("/system/xbin/which", "su"))
            val reader = BufferedReader(InputStreamReader(process.inputStream))
            reader.readLine() != null
        } catch (t: Throwable) {
            false
        } finally {
            process?.destroy()
        }
    }

    private fun getData3Type5(): Boolean {
        return getData3Type2() || getData3Type3() || getData3Type4()
    }

    fun getData3Type6(): String {
        return if (getData3Type5()) "YES" else "NO"
    }


    fun getData3Type7(context: Context): String {
        return try {
            val config = context.resources.configuration
            config.keyboard.toString().verifyStr()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getData3Type8(): String {
        val isMatch = (
                Build.BRAND.startsWith("generic") ||
                        Build.FINGERPRINT.startsWith("generic") ||
                        Build.FINGERPRINT.startsWith("unknown") ||
                        Build.HARDWARE.contains("goldfish") ||
                        Build.HARDWARE.contains("ranchu") ||
                        Build.MODEL.contains("google_sdk") ||
                        Build.MODEL.contains("Emulator") ||
                        Build.MODEL.contains("Android SDK built for x86") ||
                        Build.MANUFACTURER.contains("Genymotion") ||
                        (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) ||
                        "google_sdk" == Build.PRODUCT)

        return if (isMatch) "YES" else "NO"
    }


    @SuppressLint("MissingPermission")
    fun getData3Type9(context: Context): String {
        return ""
    }

    fun getData3Type10(context: Context): String {
        val cameraId = getData3Type12(context)
        return getData3Type14(context, cameraId)
    }

    fun getData3Type11(context: Context): String {
        val cameraId = getData3Type13(context)
        return getData3Type14(context, cameraId)
    }

    private fun getData3Type12(context: Context): Int {
        var cid = -1
        val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
        try {
            val cameraIds = cameraManager.cameraIdList
            for (cID in cameraIds) {
                if (cID == CameraCharacteristics.LENS_FACING_BACK.toString()) {
                    cid = cID.toInt()
                    break
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return cid
    }

    private fun getData3Type13(context: Context): Int {
        var cid = -1
        try {
            val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
            val cameraIds = cameraManager.cameraIdList
            for (cID in cameraIds) {
                if (cID == CameraCharacteristics.LENS_FACING_FRONT.toString()) {
                    cid = cID.toInt()
                    break
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return cid
    }

    private fun getData3Type14(context: Context, paramInt: Int): String {
        var pixelValue = "0"
        if (paramInt == -1)
            return pixelValue

        try {
            val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
            val cameraCharacteristics = cameraManager.getCameraCharacteristics(paramInt.toString())
            val streamConfigurationMap =
                cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
            val sizes = streamConfigurationMap?.getOutputSizes(ImageFormat.JPEG)
            if (!sizes.isNullOrEmpty()) {
                val fistSize = sizes[0]
                val gWidth = fistSize.width
                val gHeight = fistSize.height
                val pixels = (gWidth * gHeight / 10000)
                pixelValue = pixels.toString()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return pixelValue
    }

    fun getData3Type15(context: Context): String {
        var sizeStr = ""
        try {
            val point = Point()
            val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            wm.defaultDisplay.getRealSize(point)
            val dm = context.resources.displayMetrics
            val x = (point.x / dm.xdpi.toDouble()).pow(2.0)
            val y = (point.y / dm.ydpi.toDouble()).pow(2.0)
            val screenInches = sqrt(x + y)
            sizeStr = screenInches.toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return sizeStr
    }
}
